package br.com.maps.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import br.com.maps.ramais.R;
import br.com.maps.ui.MapsRamaisActivity;

public class TabsFragment extends Fragment {

	private MapsRamaisActivity activity;

	@Override
	public void onAttach(Activity activity) {

		super.onAttach(activity);
		this.activity = (MapsRamaisActivity) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tabs_fragment, container, false);
		TabHost tabHost = (TabHost) view.findViewById(android.R.id.tabhost);
		this.setupTabs(tabHost);
		tabHost.setOnTabChangedListener(this.activity.new TabChangedListener());
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	private void setupTabs(TabHost tabHost) {
		tabHost.setup();
		tabHost.addTab(this.newTabSpec(tabHost, "contacts", "Contatos"));
		tabHost.addTab(this.newTabSpec(tabHost, "favourites", "Favoritos"));
	}

	private TabSpec newTabSpec(TabHost tabHost, String tag, String label) {
		TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
		tabSpec.setIndicator(label);
		tabSpec.setContent(new BaseTabContentFactory(this.getActivity().getBaseContext()));
		return tabSpec;
	}

}
